﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Windows.Forms;//for messagebox
namespace STT
{
    class WinmmHelper
    {
        /// <summary>
        /// 关于wave in caps的详细说明
        /// http://msdn.microsoft.com/en-US/library/windows/desktop/dd743839(v=vs.85).aspx
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]//, Pack = 4)]
        public struct WAVEINCAPS
        {
            public ushort wMid;
            public ushort wPid;
            public uint vDriverVersion;
            //[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
            //public char[] szPname;
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 32)]
            public string szPname;
            public uint dwFormats;
            public ushort wChannels;
            public ushort wReserved1;
        }

        /// <summary>
        /// 关于wave out caps的详细说明
        /// http://msdn.microsoft.com/en-us/dd743855(v=vs.85)
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]//, Pack = 4)]
        public struct WAVEOUTCAPS
        {
            //制造商ID
            public ushort wMid;
            //产品ID
            public ushort wPid;
            //版本号，高字节是主，低字节是次
            public uint vDriverVersion;
            //产品名称
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string szPname;
            //支持的格式
            public uint dwFormats;
            //单声道(1)还是立体声(2)
            public ushort wChannels;
            //其他
            public ushort wReserved1;
            //可选的参数
            public uint dwSupport;
        }

        //声明使用winmm.dll 获取 波形输出设备的数量
        [DllImport("winmm.dll")]
        public static extern uint waveOutGetNumDevs();
        //获取波形输出设备的性能
        [DllImport("winmm.dll")]
        public static extern uint waveOutGetDevCaps(IntPtr hwo, ref WAVEOUTCAPS pwoc, uint cbwoc);

        //声明使用winmm.dll 获取 波形输入设备的数量
        [DllImport("winmm.dll")]
        public static extern uint waveInGetNumDevs();
        //获取波形输入设备的性能
        [DllImport("winmm.dll")]
        public static extern uint waveInGetDevCaps(IntPtr hwo, ref WAVEINCAPS pwoc, uint cbwoc);

        //public List<WAVEINCAPS> listWaveInDevCaps()
        //{
        //    List<WAVEINCAPS> list = new List<WAVEINCAPS>();
        //    uint waveInNum = waveInGetNumDevs();
        //    for (int i = 0; i < waveInNum; i++)
        //    {
        //        WAVEINCAPS wic = new WAVEINCAPS();
        //        uint j = waveInGetDevCaps((IntPtr)i, ref wic, (uint)Marshal.SizeOf(wic));
        //        if (j == 0)
        //        {
        //            list.Add(wic);
        //        }
        //    }
        //    return list;
        //}

        public List<WaveCaps> listWaveInDevCaps()
        {
            List<WaveCaps> list = new List<WaveCaps>();
            uint waveInNum = waveInGetNumDevs();
            for (int i = 0; i < waveInNum; i++)
            {
                WAVEINCAPS wic = new WAVEINCAPS();
                uint j = waveInGetDevCaps((IntPtr)i, ref wic, (uint)Marshal.SizeOf(wic));
                if (j == 0)
                {
                    try
                    {
                        WaveCaps wc = new WaveCaps();
                        wc.dwFormats = wic.dwFormats;
                        wc.szPname = wic.szPname;
                        wc.vDriverVersion = wic.vDriverVersion;
                        wc.wChannels = wic.wChannels;
                        wc.wMid = wic.wMid;
                        wc.wPid = wic.wPid;
                        wc.wReserved = wic.wReserved1;
                        wc.deviceID = i;
                        list.Add(wc);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
            return list;
        }

        //public List<WAVEOUTCAPS> listWaveOutDevCaps()
        //{
        //    List<WAVEOUTCAPS> list = new List<WAVEOUTCAPS>();
        //    uint waveOutNum = waveOutGetNumDevs();
        //    for (int i = 0; i < waveOutNum; i++)
        //    {
        //        WAVEOUTCAPS woc = new WAVEOUTCAPS();
        //        uint j = waveOutGetDevCaps((IntPtr)i,ref woc, (uint)Marshal.SizeOf(woc));
        //        if (j == 0)
        //        {
        //            list.Add(woc);
        //        }
        //    }
        //    return list;
        //}

        public List<WaveCaps> listWaveOutDevCaps()
        {
            List<WaveCaps> list = new List<WaveCaps>();
            uint WaveOutNum = waveOutGetNumDevs();
            for (int i = 0; i < WaveOutNum; i++)
            {
                WAVEOUTCAPS woc = new WAVEOUTCAPS();
                uint j = waveOutGetDevCaps((IntPtr)i, ref woc, (uint)Marshal.SizeOf(woc));
                if (j == 0)
                {
                    try
                    {
                        WaveCaps wc = new WaveCaps();
                        wc.dwFormats = woc.dwFormats;
                        wc.szPname = woc.szPname;
                        wc.vDriverVersion = woc.vDriverVersion;
                        wc.wChannels = woc.wChannels;
                        wc.wMid = woc.wMid;
                        wc.wPid = woc.wPid;
                        wc.wReserved = woc.wReserved1;
                        wc.dwSupport = woc.dwFormats;
                        wc.deviceID = i;
                        list.Add(wc);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
            return list;
        }
    }
}
